/**
 * Project: complex.mina
 * 
 * File Created at 2011-3-16
 * $Id: ServerHandler.java 129 2011-03-18 03:43:37Z ymkyve501@163.com $
 * 
 * Copyright 1999-2100 Alibaba.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package com.complex.mina;

import java.util.HashMap;
import java.util.Map;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

/**
 * TODO Comment of Handler
 * 
 * @author gaoliang
 * @date 2011-3-16
 */
public class ServerHandler extends IoHandlerAdapter {

    //private static Set<IoSession> sessions = Collections.synchronizedSet(new HashSet<IoSession>());
    
    private static Map<String,String> map = new HashMap<String,String>();
    @Override
    public void sessionCreated(IoSession session) throws Exception {
        System.out.println("ServerHandler sessionCreated");
    }

    @Override
    public void sessionOpened(IoSession session) throws Exception {
        System.out.println("ServerHandler sessionOpened");
    }

    @Override
    public void sessionClosed(IoSession session) throws Exception {
        System.out.println("ServerHandler sessionClosed");
    }

    @Override
    public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
        System.out.println("ServerHandler sessionIdle");
    }

    @Override
    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
        System.out.println("ServerHandler exceptionCaught");
    }

    @Override
    public void messageReceived(IoSession session, Object message) throws Exception {
        String theMessage = (String)message;
        if(theMessage.startsWith("Provider")){
            String[] strs = theMessage.split("=")[1].split("//");
            map.put(strs[0], strs[1]);
            System.out.println("ServerHandler messageReceived Provider Message:"+theMessage);
        }
        if(theMessage.startsWith("Client")){
            String[] strs = theMessage.split(" ");
            session.write(map.get(strs[1]));
            System.out.println("ServerHandler messageReceived Client Message"+theMessage);
        }
        
    }

    @Override
    public void messageSent(IoSession session, Object message) throws Exception {
        System.out.println("ServerHandler messageSent");
    }

}
